package com.coin900.coin.cmd.user;

import com.coin900.coin.persist.sop.SopUserVO;
import com.coin900.coin.service.sop.ISopUserService;
import com.google.gson.Gson;
import com.coin900.coin.base.Constants;
import com.coin900.coin.base.FuConstants;
import com.coin900.coin.base.utils.JwtUtils;
import com.coin900.coin.cmd.ACmd;
import com.coin900.coin.cmd.CmdSign;
import com.coin900.coin.code.CmdCode;
import com.coin900.coin.code.RespCode;
import com.coin900.coin.persist.fu.FuUserVO;
import com.coin900.coin.service.IQiniuService;
import com.coin900.coin.service.fu.IFuUserService;
import com.value.utils.Md5Encoder;
import net.sf.json.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 登录
 *
 * @author shui
 * @create 2017-12-08
 **/
@Controller
public class LoginCmd extends ACmd {
    @Override
    public CmdSign execute(HttpServletRequest request, CmdSign sign) throws Exception {

        CmdSign respSign = new CmdSign(sign);
        String source = sign.getSource();
        JSONObject jsonObject = JSONObject.fromObject(source);
        String username = jsonObject.getString("username");
        String password = jsonObject.getString("password");

        Subject currentUser = SecurityUtils.getSubject();
        if (super.getUser(sign) != null) {
            currentUser.logout();
        }
        Map<String, Object> params = new HashMap<>();
        params.put("username", username);
        params.put("password", Md5Encoder.generateCode(password));
        List<SopUserVO> userVOList = this.userService.loadSopUser(params);
        if (userVOList.isEmpty()) {
            respSign.setMsg("账号不存在或密码错误");
            respSign.setRespCode(RespCode.LOGIN_FAIL);
            return respSign;
        }
        SopUserVO user = userVOList.get(0);
//        username = user.getUsername();
//        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, password);
//        usernamePasswordToken.setRememberMe(true);
//        try {
//            currentUser.login(usernamePasswordToken);
//        } catch (AuthenticationException e) {
//            e.printStackTrace();
//            respSign.setMsg("登录失败");
//            respSign.setRespCode(RespCode.LOGIN_FAIL);
//            return respSign;
//        }
        //生成token
        //以ip用户名密码加时间生成token
        //返回token
        Long userId = user.getId();
        String jwt = JwtUtils.createJWT(userId, Constants.jwt_base64Security, Constants.jwt_Millis);
        respSign.setSessionToken(jwt);

        String upToken = qiniuService.getUpToken();
        user.setUpToken(upToken);
        user.setBucketDomain(FuConstants.BUCKET_DOMAIN);
        source = new Gson().toJson(user);
        respSign.setSource(source);
        return respSign;
    }


    @Autowired
    private ISopUserService userService;
    @Autowired
    private IQiniuService qiniuService;

    @Override
    public String getCmdCode() {
        return CmdCode.FU_LOGIN;
    }
}
